package components {
import com.greensock.TweenLite;
import com.greensock.TweenLite;

import events.DigComponentEvent;

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.ui.Mouse;

import ru.cjay.loading.ResourceManager;

public class WorkFieldComponent extends Sprite {
    private var _background:DisplayObject;
    private var _cells:Array;
    private var _background_cells:Sprite;
    private var _background_x:Object;
    private var _setting:int;
    private var _w:int;
    private var _h:int;

    private var _winner:Object = null;

    private var _clicked_x:int = -1;
    private var _clicked_y:int = -1;

    private var _pointer:MovieClip;

    private var _lower:Sprite;
    private var _middle:Sprite;
    private var _upper:Sprite;

    private var isPlayed:Boolean = false;

    private var bang:MovieClip;

    private var _fog:Sprite;


    private function stopPlay(event:Event):void{
        isPlayed = false;
        trace("StopMe");
        TweenLite.to(bang, 0.2,{alpha:0});
        if ((_clicked_x+_clicked_y)>-1){
            (_background_x[_clicked_x][_clicked_y]["x_label"] as MovieClip).visible = true;
            (_background_x[_clicked_x][_clicked_y]["x_label"] as MovieClip).gotoAndPlay(1);
            _clicked_x = _clicked_y = -1;
        }

    }

    public function WorkFieldComponent(p_setting:int) {
        _setting = p_setting;
        _fog = new Sprite();
         _fog = new Sprite();
        var bangClass:Class=ResourceManager.getInstance().getSwfAssetClass("main_assets_"+_setting+":Bang")as Class;
        bang = new bangClass();
        bang.addEventListener("THISMOVIECLIPISSTOPPED", stopPlay);

        _lower = new Sprite();
        _middle = new Sprite();
        _pointer = new (ResourceManager.getInstance().getSwfAssetClass("main_assets_"+_setting+":Sight") as Class)();
        _upper = new Sprite();

        var temp_back:Sprite = new Sprite();

        temp_back.graphics.beginFill(0x000000);
        temp_back.graphics.drawRect(0,0,560,560);
        temp_back.graphics.endFill();
        temp_back.alpha = 0.01;
        _middle.addChild(temp_back);


        _upper.addChild(_pointer);

        _middle.addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent){
            var pos:Point = _upper.globalToLocal(new Point(event.stageX ,event.stageY));
            _pointer.x = pos.x-_pointer.width/2;
            _pointer.y = pos.y-_pointer.height/2;
        });

        _middle.addEventListener(MouseEvent.ROLL_OVER, function(){
           Mouse.hide();
            _pointer.alpha = 1;
        });

        _middle.addEventListener(MouseEvent.ROLL_OUT, function(){
            Mouse.show();
            _pointer.alpha = 0;
        });

        _fog.graphics.beginFill(0x000000);
        _fog.graphics.drawRect(0, 0, 556, 556);
        _fog.graphics.endFill();

        addChild(_lower);
        addChild(_middle);
        addChild(_upper);


        _lower.mouseChildren = false;
        _upper.mouseChildren = false;
        _upper.mouseEnabled = false;

    }

    public function get background():DisplayObject {
        return _background;
    }

    public function set background(value:DisplayObject):void {
        if (_background!=null) removeChild(_background);
        _background = value;
        _lower.addChild(_background);
    }

    public function get cells():Array {
        return _cells;
    }

    public function setWinner(winner:Object):void{

        _winner = winner;
    }

    public function set cells(value:Array):void {
        if (_background_cells==null){
            _background_cells = new Sprite();
            _background_x = new Object();
            _h = value.length;
            _w = value[0].length;
             var itemClass:Class=ResourceManager.getInstance().getSwfAssetClass("main_assets_"+_setting+":Cell")as Class;
             var xClass:Class=ResourceManager.getInstance().getSwfAssetClass("main_assets_"+_setting+":LabelX")as Class;
            var item;
            var x_item;
            for (var i:int =0; i<_w; i++){
                _background_x[i] = new Object();
                for (var j:int=0; j<_h; j++){
                    item = new itemClass();
                    item.x = item.width*j;
                    item.y = item.height*i;
                    x_item = new xClass();
                    _background_x[i][j] = new Object();
                    _background_x[i][j]["x_label"] =x_item;
                    _background_x[i][j]["button"] =item;
                    x_item.width = item.width/3.5;
                    x_item.height = item.height/3.5;
                    x_item.gotoAndStop("end");
                    x_item.x = item.x+x_item.width/2+3;
                    x_item.y = item.y+x_item.height/2+5;
                    item.addEventListener(MouseEvent.CLICK, clickOnCell);
                    _background_cells.addChild(item);
                    _background_cells.addChild(x_item);
                }
            }
         _middle.addChild(_background_cells);
            _background_cells.width = background.width;
            _background_cells.height = background.height;
        }
        _cells = value;

        for (var row:int =0; row<_cells.length; row++ ){
            for (var column:int = 0; column<_cells[row].length; column++){
                (_background_x[row][column]["x_label"] as MovieClip).visible = (_cells[row][column]==1);
                (_background_x[row][column]["button"] as SimpleButton).enabled = (_cells[row][column]==0);
                (_background_x[row][column]["button"] as SimpleButton).visible = (_cells[row][column]==0);
            }
        }
         trace("MyWinner = ", _winner);
        if (((_clicked_x+_clicked_y)>-1) && (_winner==null)){
            (_background_x[_clicked_x][_clicked_y]["x_label"] as MovieClip).visible = false;
        }

        if (_winner!=null){
            trace("I WIN!");
            var win:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("main_assets_" + this.setting + ":Winner") as Class)();
            _background_cells.addChild(win);
            win.x = _winner.win_y*_background_x[0][0]["button"].width;
            win.y = _winner.win_x*_background_x[0][0]["button"].height;
            win.width =_background_x[0][0]["button"].width;
            win.height =_background_x[0][0]["button"].height;

        }
//        _clicked_x = _clicked_y = -1;
        TweenLite.to(_fog,0.2,{alpha:0.0, onComplete:function(){
                    if (contains(_fog)) removeChild(_fog); }
                });

    }

    private function clickOnCell(event:MouseEvent):void{

        var t_width:Number =event.target.width;
        var t_height:Number =event.target.height;

        _clicked_y = Math.floor(event.target.x/t_width+0.1);
        _clicked_x = Math.floor(event.target.y/t_height+0.1);
        _fog.alpha = 0;
        bang.x = (_clicked_y)*t_width+t_width/2-3;
        bang.y = (_clicked_x)*t_height;
        bang.alpha = 1;
        bang.gotoAndPlay(1);
        isPlayed = true;
        _background_cells.addChild(bang);
        addChild(_fog);
        TweenLite.to(_fog,0.2,{alpha:0.5});


//        trace("I click on ::: ", click_x,click_y);
        dispatchEvent(new DigComponentEvent(_clicked_x,_clicked_y))
    }

    public function get setting():int {
        return _setting;
    }

    public function set setting(value:int):void {
        _setting = value;
    }
}
}
